package com.pets.controller;


import com.pets.entity.BountyList;
import com.pets.entity.FileInformation;
import com.pets.entity.Users;
import com.pets.service.IBountyListService;
import com.pets.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 悬赏表 前端控制器
 * </p>
 *
 * @author 啦啦啦
 * @since 2024-04-09
 */
@RestController
@RequestMapping("/bounty-list")
@Api(tags = "悬赏表接口")
public class BountyListController {

    @Autowired
    private IBountyListService bountyListService;

    @ApiOperation("发布悬赏（悬赏名称bountyListName，介绍introduce，积分bountyListIntegral，当前登陆人userId）")
    @PostMapping
    public Result register(@RequestBody BountyList bountyList, HttpSession session) {
        /*Object userObject = session.getAttribute("users");
        if (userObject instanceof Users) {
            Users user = (Users) userObject;
            System.out.println(user.getUsername()+"/"+user.getUserId());
            bountyList.setUserId(user.getUserId());
            return bountyListService.register(bountyList);
        }else {
            return Result.fail("未登录！");
        }*/

        return bountyListService.register(bountyList);
    }

    @ApiOperation("修改悬赏信息（同样只能修改bountyListName字段，但是isItCompleted显示为true就不可以修改）")
    @PutMapping("/update")
    public Result update(@RequestBody BountyList bountyList,HttpSession session) {
        /*Object userObject = session.getAttribute("users");
        if (userObject instanceof Users) {
            Users user = (Users) userObject;
            System.out.println(user.getUsername() + "/" + user.getUserId());
            if (!user.getIsAdministrators() && bountyList.getUserId() != user.getUserId()) {
                return Result.fail("你不能修改别人发布的悬赏！");
            }
            return bountyListService.updateData(bountyList);
        } else {
            return Result.fail("未登录！");
        }*/

        return bountyListService.updateData(bountyList);
    }

    @ApiOperation("根据id删除悬赏")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id,@PathVariable Integer userId,HttpSession session) {
        /*Object userObject = session.getAttribute("users");
        if (userObject instanceof Users) {
            Users user = (Users) userObject;
            System.out.println(user.getUsername()+"/"+user.getUserId());
            return bountyListService.deleteById(id,user.getUserId());
        }else {
            return Result.fail("未登录！");
        }*/
        return bountyListService.deleteById(id,userId);
    }

    @ApiOperation("分页查询悬赏")
    @GetMapping("/listPage")
    public Result listPage(@ApiParam(value = "页码", required = true)@RequestParam(defaultValue = "1") Integer page,
                           @ApiParam(value = "页数", required = true)@RequestParam(defaultValue = "10") Integer pageSize,
                           @ApiParam(value = "悬赏名称")@RequestParam(required = false) String bountyListName,
                           @ApiParam(value = "用户id（管理员不用传）")@RequestParam(required = false) Integer userId) {
        return bountyListService.selectPage(page, pageSize, bountyListName, userId);
    }
}
